package org.dousb.doupicture.interfaces.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dousb.doupicture.application.service.UserApplicationService;
import org.dousb.doupicture.domain.user.constant.UserConstant;
import org.dousb.doupicture.domain.user.entity.User;
import org.dousb.doupicture.infrastructure.annotation.AuthCheck;
import org.dousb.doupicture.infrastructure.common.BaseResponse;
import org.dousb.doupicture.infrastructure.common.DeleteRequest;
import org.dousb.doupicture.infrastructure.common.ErrorCode;
import org.dousb.doupicture.infrastructure.common.ResultUtils;
import org.dousb.doupicture.infrastructure.exception.ThrowUtils;
import org.dousb.doupicture.interfaces.assembler.UserAssembler;
import org.dousb.doupicture.interfaces.dto.user.*;
import org.dousb.doupicture.interfaces.vo.user.LoginUserVO;
import org.dousb.doupicture.interfaces.vo.user.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserApplicationService userApplicationService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        long result = userApplicationService.userRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        LoginUserVO loginUserVO = userApplicationService.userLogin(userLoginRequest, request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userApplicationService.getLoginUser(request);
        return ResultUtils.success(userApplicationService.getLoginUserVO(loginUser));
    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        boolean userLogout = userApplicationService.userLogout(request);
        return ResultUtils.success(userLogout);
    }

    /**
     * 添加用户
     * 仅管理员使用
     *
     * @param userAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.PARAMS_ERROR);
        User userEntity = UserAssembler.toUserEntity(userAddRequest);
        return ResultUtils.success(userApplicationService.addUser(userEntity));
    }

    /**
     * 删除用户
     * 仅管理员可用
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        boolean remove = userApplicationService.deleteUser(deleteRequest);
        return ResultUtils.success(remove);
    }

    /**
     * 更新用户
     * 仅管理员可用
     *
     * @param userQueryRequest
     * @return
     */
    @PostMapping("update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest updateRequest) {
        ThrowUtils.throwIf(updateRequest == null, ErrorCode.PARAMS_ERROR);
        User user = UserAssembler.toUserEntity(updateRequest);
        userApplicationService.updateById(user);
        return ResultUtils.success(true);
    }

    /**
     * 根据id获取用户
     * 仅管理员可用
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userApplicationService.getUserById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(user);
    }


    /**
     * 根据id获取用户包装类
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(long id, HttpServletRequest request) {
        return ResultUtils.success(userApplicationService.getUserVOById(id));
    }

    /**
     * 分页获取用户包装列表
     * 仅管理员可用
     *
     * @param userQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> listUserVOPage(@RequestBody UserQueryRequest userQueryRequest) {
        Page<UserVO> userVOPage = userApplicationService.page(userQueryRequest);
        return ResultUtils.success(userVOPage);
    }

}
